package com.hckj.jianzhijia.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.hckj.jianzhijia.JianzhijiaApplacation;
import com.hckj.jianzhijia.JobDetailsActivity;
import com.hckj.jianzhijia.PublishActivity;
import com.hckj.jianzhijia.R;
import com.hckj.jianzhijia.adapters.JobListAdapter;
import com.hckj.jianzhijia.popupwindows.JobApprovingPopWin;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.StateConstant;
import com.hckj.jianzhijia.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;


public class ApprovingJFragment extends ListViewFragment implements JobApprovingPopWin.PopWinInterface {


    AVQuery<AVObject> query = null;
    private int[] colorResource = {R.color.color_primary};
    private JobListAdapter jobListAdapter;
    private ArrayList<AVObject> avObjectArrayList;
    private View parentView;


    public ApprovingJFragment() {
    }

    public static ApprovingJFragment newInstance(View parentView) {
        ApprovingJFragment fragment = new ApprovingJFragment();
        Bundle args = new Bundle();
        fragment.setParentView(parentView);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        query = new AVQuery<AVObject>("job");
        avObjectArrayList = new ArrayList<AVObject>();
        jobListAdapter = new JobListAdapter(getActivity(), avObjectArrayList);
        ((JianzhijiaApplacation)(getActivity().getApplication())).addJobListFreshInterfaces(this);
        searchJob();
    }


    @Override
    public int getCustomContentView() {
        return 0;
    }

    @Override
    public JobListAdapter getListAdapter() {
        return jobListAdapter;
    }

    @Override
    public boolean useCustomContentView() {
        return false;
    }

    @Override
    public boolean pullToRefreshEnabled() {
        return true;
    }

    @Override
    public int[] getPullToRefreshColorResources() {
        return colorResource;
    }

    @Override
    public void onRefresh() {
        searchJob();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        Intent intent = new Intent(getActivity(), JobDetailsActivity.class);
        intent.putExtra("JobAVObject", avObjectArrayList.get(position));
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
        JobApprovingPopWin jobApprovingPopWin = new JobApprovingPopWin(getActivity(), this, parentView,position);
        jobApprovingPopWin.showAtLocation(parentView, Gravity.CENTER, 0, 0);
        return true;
    }


    public void searchJob() {
        setRefreshing(true);
        query.whereEqualTo("publishAVUser", AVUser.getCurrentUser());
        query.whereEqualTo("jobState", StateConstant.JOB_STATE_PUBLISHING);
        query.setLimit(20);
        query.orderByDescending("createdAt");//降序排列
        query.findInBackground(new FindCallback<AVObject>() {
            public void done(List<AVObject> avObjects, AVException e) {
                if (e == null) {
                    avObjectArrayList.clear();
                    avObjectArrayList.addAll(avObjects);
                    jobListAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(getActivity(), ExceptionUtils.getExceptionInfo(e));
                }
                setRefreshing(false);
            }
        });

    }


    @Override
    public void jobDelete(final int position) {
        AVObject avObject = avObjectArrayList.get(position);
        avObject.put("jobState", StateConstant.JOB_STATE_PUBLISHING_DELETE);
        avObject.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    ToastUtils.showToast(getActivity(), "删除成功");
                    avObjectArrayList.remove(position);
                    jobListAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(getActivity(), "删除失败  " + ExceptionUtils.getExceptionInfo(e));
                }
            }
        });

    }

    @Override
    public void jobModify(int position) {
        AVObject avObject = avObjectArrayList.get(position);
        Intent intent = new Intent(getActivity(), PublishActivity.class);
        intent.putExtra("job",avObject);
        intent.putExtra("ifModify", true);
        startActivity(intent);
    }

    public void setParentView(View parentView) {
        this.parentView = parentView;
    }


    @Override
    public void afterUpdateListFresh() {
        searchJob();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((JianzhijiaApplacation)(getActivity().getApplication())).removeJobListFreshInterface(this);
    }
}
